package pers.lws.erp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.lws.erp.common.emum.IDPrefix;
import pers.lws.erp.common.emum.MsgStatusEmum;
import pers.lws.erp.common.result.ResultBody;
import pers.lws.erp.entity.ProductionPlan;
import pers.lws.erp.service.IProductionPlanService;
import pers.lws.erp.utils.GeneratorID;

import java.util.List;

/**
 *
 * @author liuws
 * @since 2020-09-22
 */
@RestController
@RequestMapping("/productionPlan")
@Slf4j
public class ProductionPlanController {

    @Autowired
    private IProductionPlanService productionPlanService;

    @PostMapping("/maketProductionRegister")
    public ResultBody maketRegister(@RequestBody ProductionPlan productionPlan) {

        ProductionPlan maxId = productionPlanService.getMaxId();
        if (maxId == null || maxId.getProductionId() == "") {
            productionPlan.setProductionId(GeneratorID.getId(IDPrefix.PRODUCTION.getPrefix()));
        } else {
            productionPlan.setProductionId(GeneratorID.getId(maxId.getProductionId()));
        }

        boolean save = productionPlanService.save(productionPlan);
        return save ? ResultBody.success(MsgStatusEmum.ADD_SUCCESS.getMsg(),productionPlan) : ResultBody.error(MsgStatusEmum.ADD_ERROR.getMsg());
    }

    @GetMapping("/findProductionPlan")
    public ResultBody findProductionPlan() {
        QueryWrapper<ProductionPlan> tWrapper = new QueryWrapper<>();
        tWrapper.eq("checked", 1);
        List<ProductionPlan> list = productionPlanService.list(tWrapper);
        if (!list.isEmpty()) {
            return ResultBody.success(MsgStatusEmum.OTHER_SUCCESS.getMsg(), list);
        }else {
            return ResultBody.error(MsgStatusEmum.OTHER_ERROR.getMsg());
        }
    }

    @PostMapping("/updateCheckedStatus/{productionId}/{checked}")
    public ResultBody updateCheckedStatus(@PathVariable String productionId,@PathVariable Integer checked) {
        return productionPlanService.updateCheckedStatus(productionId, checked) ? ResultBody.success("已审核！",null):ResultBody.error("审核失败！");
    }

}

